package algorithms.que14;

import java.util.Arrays;

/**
 * @功能 3lgN
 * @创建时间 2020/9/12
 */
public class E1d4d20c {
    public void arrsort(int[] arr, int a){
        int[] arr1=arr.clone();
        int last =arr.length-1;
        Arrays.sort(arr1); //lgN
  //      System.out.println(arr[0]+arr1[0]);
        if(arr[0]==arr1[last]||arr[last]==arr1[last]){
            add(arr,a,arr1[0],arr[last]);
//            dec(arr,a,arr[0],arr1[0]);
        }
        if(arr[0]==arr1[0]||arr[last]==arr1[0]){
            add(arr,a,arr[0],arr1[last]);
//            dec(arr,a,arr1[last],arr[last]);
        }
    }
    public void add(int[] arr, int a,int min,int max){ //lgN
        int fi=0;
        int  la=arr.length-1;
       while (fi<la){
           int mid=fi+(la-fi)/2;
           if(arr[mid]>a){
               la=mid-1;
           }else if(arr[mid]<a){
               fi=mid+1;
           }else if(arr[mid]==a){
               System.out.println(mid);
               break;
           }
       }

        dec(arr, a, min, max);

    }
    public  int max(int[] a,int lo,int hi)
    {
        if(lo==hi) return lo;
        int mid=lo+(hi-lo)/2;
        if(a[mid]<a[mid+1])
            return max(a,mid+1,hi);
        else if(a[mid]>a[mid+1])
            return max(a,lo,mid);
        else
            return mid;
    }
    public void dec(int[] arr, int a,int min,int max){ //lgN
        int fi=0;
        int la=arr.length-1;
        while (fi<la){
            int mid=fi+(la-fi)/2;
            if(arr[mid]>a){
                fi=mid+1;
            }else if(arr[mid]<a){
                la=mid-1;
            }else if(arr[mid]==a){
                System.out.println(mid);
                break;
            }else {
                System.out.println(-1);
                break;
            }
        }
    }
}
class client2{
    public static void main(String[] args) {
        E1d4d20c a = new E1d4d20c();
        int[] arr = {1,2,8,6,4,3,0};
        int max=a.max(arr,0,arr.length);
        a.arrsort(arr,8);
    }
}